package com.hydl.power.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hydl.power.mapper.SysMenuMapper;
import com.hydl.power.mapper.SysRoleMapper;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.*;
import com.hydl.power.service.SysUserService;
import com.hydl.power.utils.MD5Util;
import com.hydl.power.utils.TableUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public int addUser(String userName,String card,Integer status,Integer projectId,Integer mapStatus ) {
        SysUser user = new SysUser();
        user.setFlag(1);
        user.setPassword(MD5Util.crypt("123456"));
        user.setUsername(userName);
        user.setCard(card);
        user.setToken(getItemID());
        user.setStatus(status);
        user.setProjectId(projectId);
        user.setMapStatus(mapStatus);
        return userMapper.addUser(user);
    }

    @Override
    public SysUser getUserByToken(String token) {

        return userMapper.getUserByToken(token);
    }

    @Override
    public Set<SysMenuVO> getMenu(Integer userId) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        List<SysMenu> list = sysMenuMapper.getListByUserId(userId);
        if (list == null || list.isEmpty()) {
            return null;
        }
        Set<SysMenuVO> menuVOList = new LinkedHashSet<>();
        Map<Integer, SysMenuVO> map1 = new LinkedHashMap<>(32);
        Map<Integer, SysMenu> map2 = new LinkedHashMap<>(32);
        for (SysMenu sysMenu : list) {
            if (sysMenu.getpId() == 0) {
                map1.put(sysMenu.getId(), cast(sysMenu, sysMenu.getId()));
            } else {
                map2.put(sysMenu.getId(), sysMenu);
            }
        }
        if (!map2.isEmpty()) {
            for (Integer str : map2.keySet()) {
                SysMenu sysMenu = map2.get(str);
                SysMenuVO vo = map1.get(sysMenu.getpId());
                /*if (sysMenu.getpId().equals(menuId)) {
                    vo.setOpen("open");
                }*/
                if (vo.getChilds() == null || vo.getChilds().isEmpty()) {
                    LinkedHashSet<SysMenuVO> set = new LinkedHashSet<>();
                    set.add(cast(sysMenu, sysMenu.getId()));
                    vo.setChilds(set);
                } else {
                    vo.getChilds().add(cast(sysMenu, sysMenu.getId()));
                }
            }
        }
        for (Integer str : map1.keySet()) {
            menuVOList.add(map1.get(str));
        }
        return menuVOList;
    }

    @Override
    public SysUser getUser(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return null;
        }
        return userMapper.selectByUserName(userName);
    }


    @Override
    public int updatePassword(Integer id, String password) {
        return userMapper.updatePassword(id, password);
    }

    @Override
    public TableUtils getList(Integer pageNum, Integer pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<SysUser> list = userMapper.getList( pageNum,  pageSize);
        TableUtils tableUtils = new TableUtils();
        tableUtils.setTotal(page.getTotal());
        tableUtils.setRows(list);
        return tableUtils;
    }

    /*@Override
    public PageInfo<SysUser> getAll(int page, int rows) {
        List<SysUser> list = userMapper.getList(page, rows);
        return new PageInfo<>(list);
    }*/

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public int updateUserById(Integer id,String username,String card,Integer flag) {
        return userMapper.updateUserById(id,username,card,flag);
    }

    @Override
    public int updateLoginTime(String userName) {
        return userMapper.updateLoginTime(userName);
    }

    @Override
    public SysUser getAllById(Integer id) {
        return userMapper.getAllById(id);
    }

    @Override
    public List<SysUser> getAllUser() {
        return userMapper.getAllUser();
    }


    private static SysMenuVO cast(SysMenu sysMenu, Integer menuId) {
        SysMenuVO vo = new SysMenuVO();
        vo.setId(sysMenu.getId());
        vo.setName(sysMenu.getName());
        vo.setPath(sysMenu.getPath());
        vo.setIcon(sysMenu.getIcon());
        vo.setOpen("");
        if (menuId.equals(sysMenu.getId())) {
            vo.setActive("active");
        } else {
            vo.setActive("");
        }
        return vo;
    }


    private static String getItemID(){
        String val = "";
        Random random = new Random();
        for ( int i = 0; i < 10; i++ ){
            String str = random.nextInt( 2 ) % 2 == 0 ? "num" : "char";
            if ( "char".equalsIgnoreCase( str ) ){
                // 产生字母
                int nextInt = random.nextInt( 2 ) % 2 == 0 ? 65 : 97;
                val += (char) ( nextInt + random.nextInt( 26 ) );
            }else if ( "num".equalsIgnoreCase( str ) ){
                // 产生数字
                val += String.valueOf( random.nextInt( 10 ) );
            }
        }
        return val;
    }

}
